/**
 * Created by Administrator on 2016/9/20.
 */
(function($) {
    $(function () {

          //获得cookie;
         if(getCookie("inpr")!==undefined){
               $(".str").html(getCookie("inpr"));
               console.log(getCookie("inpr"));
           }else{
               $(".str").html(0);
           }

           var opaxt= getCookie("username"),
              kag= getCookie("flag"),
              numbat1= getCookie("numb");
            if(numbat1==undefined){
                numbat1=0;
            }
            if(getCookie("username")!==undefined&&kag==1){
                var sTrra= JSON.parse(getCookie("username"));

                $(".idcard").html(sTrra[numbat1].name);
                $(".idcard").css({display:"block"})
                $(".tuichu").css({display:"block"});
                $(".delu").css({display:"none"});
                $(".zhuce").css({display:"none"})
                $(".tuichu").html("退出");
            }else{
                $(".idcard").css({display:"none"});
                $(".tuichu").css({display:"none"});
                $(".delu").css({display:"block"});
                $(".zhuce").css({display:"block"});
            }

        $(".tuichu").on("click",function(){
            setCookie("flag",0,7,"/")
            $(".idcard").css({display:"none"});
            $(".tuichu").css({display:"none"});
            $(".delu").css({display:"block"});
            $(".zhuce").css({display:"block"});
        })

        //划过
        $(".ulpatp .li1").mouseenter(function () {

            $(".li1 .weix").css({display: "block"});
        });
        $(".ulpatp .li1").mouseleave(function () {
            $(".li1 .weix").css({display: "none"});
        })
        var oShopcar= $(".shopcar"),
            oShopAst= $(".astpac"),
            oImgase = $(".gm2"),
            oPrices=  $(".aspan"),
            iIndexcar = 0,
             number1= 0;
        oShopcar.on("click",function(){
            number1++;
            iIndexcar= oShopcar.index($(this));
            var bBtn = true;
            var oAttr1 = getCookie('attr1');

            if(oAttr1 === undefined){
                var shopCar1 =[];
            }else{
                var shopCar1 = JSON.parse(oAttr1) ;
            }
            for(var i=0;i<shopCar1.length;i++){
                if(shopCar1[i].attrName1 == oShopAst.eq(iIndexcar).html()){
                    var num1 = parseInt(shopCar1[i].attrNum);
                    num1 += number1;
                    shopCar1[i].attrNum = num1;
                    bBtn = false;
                }
            }
            if(bBtn){
                var attr1 = {
                    attrName1: oShopAst.eq(iIndexcar).html(),
                    attrPrice1: oPrices.eq(iIndexcar).html(),
                    attrSrc1: oImgase.eq(iIndexcar).attr("src"),
                    attrNum1: 	number1,

                };
                shopCar1.push(attr1);
                console.log(shopCar1);
            }
            setCookie('attr1',JSON.stringify(shopCar1),7,"/");

        })



        var
            oNav = $('.nav-bt'),
            aListLi = $('.list li'),
            aListLiA = $('.list li a'),
            aMenuConDiv = $('.dies div'),
            iIndex = 0;

        aListLi.mouseenter(function () {
            iIndex = $(this).index();
            if (iIndex == 0) {
                aListLi.removeClass('active').eq(iIndex).addClass('active');
                aListLiA.css({color: "#333"}).eq(iIndex).css({color: "#fff"});
                aMenuConDiv.css({display: 'none'});
            } else {


                aListLi.removeClass('active').eq(iIndex).addClass('active');
                aListLiA.css({color: "#333"}).eq(iIndex).css({color: "#fff"});
                aMenuConDiv.css({display: 'none'}).eq(iIndex).css({display: 'block'});
            }
        });
        oNav.mouseleave(function () {
            aListLi.eq(iIndex).removeClass('active');
            aListLiA.eq(iIndex).css({color: "#333"});
            aMenuConDiv.eq(iIndex).css({display: 'none'});
        });

        var
            olistT = $(".listp li"),
            oBtn = $(".btn-list a"),
            iIdex = 0,
            iTime = null;
        clearInterval(iTime);
        oBtn.on("mouseenter", function () {
            clearInterval(iTime);
            iIdex = $(this).index();
            olistT.animate({
                opacity: 0,
            }, function () {
                $(this).css({display: "none"});
            });
            olistT.eq(iIdex).css({display: "block"}).stop(true).animate({opacity: 1});
            oBtn.removeClass("active").eq(iIdex).addClass("active");
        });

        oBtn.on("mouseleave", function () {
            iTime = setInterval(function () {
                iIdex++;
                if (iIdex > olistT.length - 1) {
                    iIdex = 0;
                }
                olistT.animate({
                    opacity: 0,
                }, function () {
                    $(this).css({display: "none"});
                });
                olistT.eq(iIdex).css({display: "block"}).stop(true).animate({opacity: 1});
                oBtn.removeClass("active").eq(iIdex).addClass("active");
            }, 3000);

        });
        iTime = setInterval(function () {
            iIdex++;
            if (iIdex > olistT.length - 1) {
                iIdex = 0;
            }
            olistT.animate({
                opacity: 0,
            }, function () {
                $(this).css({display: "none"});
            });
            olistT.eq(iIdex).css({display: "block"}).stop(true).animate({opacity: 1});
            oBtn.removeClass("active").eq(iIdex).addClass("active");
        }, 3000);

        var
            iMd2 = $(".da2-le .imd2"),
            iMp = $(".da2-le p"),
            oDa2 = $(".da2 .da2-le"),
            iIndenx = 0;

        oDa2.on("mouseenter", function () {
            iIndenx = $(this).index();
            iMd2.eq(iIndenx).animate({top: "229"}, 300, function () {
                iMp.eq(iIndenx).animate({top: "225"}, 300);
            });
        });
        oDa2.on("mouseleave", function () {
            iMp.eq(iIndenx).animate({top: "300"}, 300);
            iMd2.eq(iIndenx).stop(true).animate({top: "269"}, 500);

        });

        var oMove = $(".move"),
            iIdnnex = 0;
        oMove.on("mouseenter", function () {
            iIdnnex = oMove.index($(this));
            oMove.css({borderColor: "#fff"}).eq(iIdnnex).css({borderColor: "red"});

        });
        oMove.on("mouseleave", function () {
            oMove.eq(iIdnnex).css({border: "1px solid #fff"});
        });

        var

            oBtShop = $(".le-ri .shop"),
            oBtA = $(".le-ri .aa"),
            iIdex4 = 0,
            iTimerq = 0;
        oBtA.on("click", function () {
            iIdex4 = oBtA.index($(this));
            iTimerq = setTimeout(function () {
                oBtShop.css({display: "none"}).eq(iIdex4).css({display: "block"});
            }, 2000);

            iTimerq = setTimeout(function () {
                oBtShop.eq(iIdex4).css({display: "none"});
            }, 4000);
        });
        //左侧栏
        $(window).on("scroll", function () {
            var
                scrollTop = document.body.scrollTop || document.documentElement.scrollTop,
                oFixedP = $(".fix-right .p3"),
                oFixedR = $(".fix-right"),
                oFixedL = $(".fix-left"),
                oFixedLp = $(".fix-left p"),
                oFixedLP1 = $(".fix-left .p1 em"),
                oFixedLP2 = $(".fix-left .p2 em"),
                oFixedLP3 = $(".fix-left .p3 em"),
                oFixedLP4 = $(".fix-left .p4 em"),
                oFixedLP5 = $(".fix-left .p5 em"),
                oFixedLP6 = $(".fix-left .p6 em"),
                timer = null,
                iIdeXn = 0,
                oFixH = $(".fixhead");

            oFixedP.on("click", function () {
                timer = setInterval(function () {
                    document.body.scrollTop -= 20;
                    if (document.body.scrollTop <= 0) {
                        clearInterval(timer);

                    }
                }, 100)
            });
/*
            oFixedLp.click(function(){
                iIdeXn=$(this).index();

                timer = setInterval(function () {
                    document.body.scrollTop -= 20;
                    if (document.body.scrollTop<0) {
                        clearInterval(timer);

                    }
                }, 100)
            });
            */


            if (scrollTop >= 500) {
                oFixH.css({display: "block"});
                oFixedR.css({display: "block"}).stop(true).animate({opacity: "1"}, 500);
                oFixedL.css({display: "block"}).stop(true).animate({opacity: "1"}, 500);

            } else {
                oFixH.css({display: "none"});
                oFixedL.animate({opacity: "0"}, 100, function () {
                    oFixedL.css({display: "none"})
                });
                oFixedR.animate({opacity: "0"}, 100, function () {
                    oFixedL.css({display: "none"})
                });

            }

            /* var iIdenx6= 0,
             tiMer=null;
             oFixedLp.on("click",function(){
             iIdenx6=oFixedLp.index($(this));
             tiMer=setInterval(function(){document.body.scrollTop-=20;
             if(document.body.scrollTop==500){
             clearInterval(tiMer);
             return false;
             }
             },100)
             });*/

            move(500, 750, oFixedLP1);
            move(750, 950, oFixedLP2);
            move(950, 1500, oFixedLP3);
            move(1500, 2000, oFixedLP4);
            move(2000, 2600, oFixedLP5);
            move(2600, 5000, oFixedLP6);

            function move(first, last, oFixed) {
                if (scrollTop >= first && scrollTop < last) {
                    oFixed.css({zIndex: "4"});
                } else {
                    oFixed.css({zIndex: "10"});
                }
            }

            /*function mop(oFixed,time){
             oFixed.on("click",function(){
             timer=setInterval(function(){document.body.scrollTop-=20;
             if(document.body.scrollTop==time){
             clearInterval(timer);
             }
             },100)
             });
             }*/
        });
        //时间抢购
        /* var ashi=23;
         var afen=59;
         var amiao=59;
         var hmiao=9;
         setInterval(function(){
         hmiao-=1;
         if(hmiao==0){
         hmiao=9;
         amiao-=1;
         if(amiao==0){
         i=60;
         afen-=1;
         if(afen==0){
         afen=59;
         ashi-=1
         }
         }}
         $('.da1-ri')[0].innerHTML="<span class='spa1'>"+剩余时间+":"+"</span><span class='spa2'>"+ashi+"</span><span class='spa2 spa3'>"+afen+"</span>+"

         },100);
         */


        //跨域
        $(".iup").bind("input propertychange", function () {
            if ($(this).val() !== "") {
                $.ajax({
                    url: "https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?&json=1&p=3&req=2&csor=1&",
                    jsonp: "cb",
                    dataType: "jsonp",
                    data: {
                        wd: $(this).val()
                    },
                    success: function (data) {
                        var sHtml = "";
                        data.s.forEach(function (v, k) {
                            sHtml += "<li><a href='javascript:;'>" + v + "</a></li>"
                        });
                        $(".search").html(sHtml);
                        $('.search li').on("mouseenter", function () {
                            iIndenx5 = $(this).index();
                            $('.search li').removeClass("active").eq(iIndenx5).addClass("active");

                        });
                        return false;
                    }
                })
            }
            var iIndenx5 = -1;
        });

        var iIdenx5 = -1;

        $('.iup').on("keydown", function (evt) {
            var event = evt || window.event;

            if ($(".search li").length && (event.keyCode === 38 || event.keyCode === 40)) {
                if (event.keyCode === 38) {
                    iIdenx5--;
                    if (iIdenx5 < 0) {
                        iIdenx5 = $(".search li").length - 1;
                    }
                } else {
                    iIdenx5++;
                    if (iIdenx5 == $(".search li").length) {
                        iIdenx5 = 0;
                    }
                }
                $(".search li").removeClass("active").eq(iIdenx5).addClass("active");
                $('.iup')[0].value = $(".search li")[iIdenx5].innerText;
                return false;
            }


        });


        $('.iup').on("focus", function () {
            $(".search").css({display: "block"})
        });
        $('.iup').on("blur", function () {
            $(".search").css({display: "none"});
        });


    })
})(jQuery);